{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Population mean: 0.000443353825615\n",
      "Population standard deviation: 0.00784267293815\n"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import quandl\n",
    "spy_table = quandl.get('LSE/SPY5')\n",
    "spy_total = spy_table[['Last Close']]\n",
    "spy_log_return = np.log(spy_total['Last Close']).diff().dropna()\n",
    "print 'Population mean:', np.mean(spy_log_return)\n",
    "print 'Population standard deviation:',np.std(spy_log_return)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 days sample returns: 0.000845189915474\n",
      "10 days sample standard deviation: 0.00313558001122\n",
      "1000 days sample returns: 0.000462827047221\n",
      "1000 days sample standard deviation: 0.00766589174299\n"
     ]
    }
   ],
   "source": [
    "print '10 days sample returns:', np.mean(spy_log_return.tail(10))\n",
    "print '10 days sample standard deviation:', np.std(spy_log_return.tail(10))\n",
    "print '1000 days sample returns:', np.mean(spy_log_return.tail(1000))\n",
    "print '1000 days sample standard deviation:', np.std(spy_log_return.tail(1000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 days 95% confidence inverval: (-0.0010982627102681939, 0.002788642541217079)\n",
      "1000 days 95% confidence inverval: (-1.230984558013321e-05, 0.00093796394002165957)\n"
     ]
    }
   ],
   "source": [
    "bottom_1 = np.mean(spy_log_return.tail(10))-1.96*np.std(spy_log_return.tail(10))/(np.sqrt(len((spy_log_return.tail(10)))))\n",
    "upper_1 = np.mean(spy_log_return.tail(10))+1.96*np.std(spy_log_return.tail(10))/(np.sqrt(len((spy_log_return.tail(10)))))\n",
    "bottom_2 = np.mean(spy_log_return.tail(1000))-1.96*np.std(spy_log_return.tail(1000))/(np.sqrt(len((spy_log_return.tail(1000)))))\n",
    "upper_2 = np.mean(spy_log_return.tail(1000))+1.96*np.std(spy_log_return.tail(1000))/(np.sqrt(len((spy_log_return.tail(1000)))))\n",
    "print '10 days 95% confidence inverval:', (bottom_1,upper_1)\n",
    "print '1000 days 95% confidence inverval:', (bottom_2,upper_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean_10      0.000845\n",
      "std_10       0.003136\n",
      "mean_1000    0.000463\n",
      "std_1000     0.007666\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "mean_10 = np.mean(spy_log_return.tail(10))\n",
    "std_10 = np.std(spy_log_return.tail(10))\n",
    "mean_1000 = np.mean(spy_log_return.tail(1000))\n",
    "std_1000 = np.std(spy_log_return.tail(1000))\n",
    "s = pd.Series([mean_10,std_10,mean_1000,std_1000],index = ['mean_10', 'std_10','mean_1000','std_1000'])\n",
    "print s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(-0.00039756352254768874, 0.00039756352254768874)\n"
     ]
    }
   ],
   "source": [
    "bottom = 0 - 1.64*std_1000/np.sqrt(1000)\n",
    "upper = 0 + 1.64*std_1000/np.sqrt(1000)\n",
    "print (bottom, upper)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(-0.00047513689280089639, 0.00047513689280089639)\n"
     ]
    }
   ],
   "source": [
    "bottom = 0 - 1.96*std_1000/np.sqrt(1000)\n",
    "upper = 0 + 1.96*std_1000/np.sqrt(1000)\n",
    "print (bottom, upper)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.90922032428\n"
     ]
    }
   ],
   "source": [
    "print np.sqrt(1000)*(mean_1000 - 0)/std_1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.025659656888\n"
     ]
    }
   ],
   "source": [
    "import scipy.stats as st\n",
    "print (1 - st.norm.cdf(1.9488))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "z-score =  2.19793023185\n",
      "p_value =  0.0139770390655\n"
     ]
    }
   ],
   "source": [
    "mean_1200 = np.mean(spy_log_return.tail(1200))\n",
    "std_1200 = np.std(spy_log_return.tail(1200))\n",
    "z_score = np.sqrt(1200)*(mean_1200 - 0)/std_1200\n",
    "print 'z-score = ',z_score\n",
    "p_value = (1 - st.norm.cdf(z_score))\n",
    "print 'p_value = ',p_value"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
